(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(['HteOS/metro/Group','HteOS/metro/Tile'],factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @class HteOS.metro.View
	 * @singleton
	 * */
	HteOS.metro.View = {
		
		groups : {},
		
		tiles : {},
		
		init : function(){
			var view = this;
			
			this.initGroups(HteOS.app.AppManager.groups);
			
			HteOS.EventManager.on("hte.group.create",function(group){
				view.addGroup(group);
			});
			HteOS.EventManager.on("hte.group.destroy",function(group){
				view.destroyGroup(group);
			});
			HteOS.EventManager.on("hte.group.app.insert",function(group,app,index){
				view.insertTileByApp(group,app,index);
			});
			HteOS.EventManager.on("hte.group.rename",function(group,oldName,newName){
				view.getGroup(group.id).setName(newName);
			});
			HteOS.EventManager.on("hte.app.resize",function(app,size){
				view.getTile(app.id).setSize(size);
			});
			HteOS.EventManager.on("hte.app.hide",function(app){
				view.getTile(app.id).hide();
			});
			HteOS.EventManager.on("hte.app.show",function(app){
				view.getTile(app.id).show();
			});
			HteOS.EventManager.on("hte.app.uninstall",function(app){
				view.destoryTile(app.id);
			});
			HteOS.EventManager.on("hte.app.install",function(app){
				view.createTile(app);
			});
		},
		
		createTile : function(app){
			var tile = new HteOS.metro.Tile(app);
			this.tiles[app.id] = tile;
			return tile.getEl();
		},
		
		getTile : function(id){
			return this.tiles[id];
		},
		
		destoryTile : function(id){
			this.getTile(id).destroy();
			delete this.tiles[id];
		},
		
		insertTileByApp : function(group,app,index){
			//var group = this.getGroup(group.id);
			//var tile = this.getTile(app.id);
			//group.insert(tile,index)	;
			this.insertTile(group,this.getTile(app.id),index);
		},
		
		insertTile : function(group,tile,index){
			this.getGroup(group.id).insert(tile,index)	;
		},
		
		initGroups : function(groups){
			for(var id in groups){
				this.addGroup(groups[id]);
				groups[id].collect();
			}
		},
		
		destroyGroup : function(group){
			this.getGroup(group.id).destroy();
		},
		
		getGroup : function(id){
			return this.groups[id];
		},
		
		addGroup : function(g){
			var container = HteOS.Metro.getEl().find(".hte-metro-inner");
			var group = new HteOS.metro.Group(g);
			this.groups[g.id] = group;
			group.render(container);
			if(HteOS.Metro.rendered){
				group.el.css({
			       transform: "translateX(0px) scale(1) perspective(300px)",
 		           opacity: 1
  				});
			}
		},
		
		/*
		renderGroup : function(container){
			var groups = HteOS.AppManager.groups;
			for(var groupId in groups){
				var group = groups[groupId];
				if(group.length() > 0){
					group.render(container);
				}
			}

		},*/
	
		sortGroup : function(){
			var me = this,el = HteOS.Metro.getEl(),fireEvent = false;
			var groups = el.find(".hte-metro-group");
			groups.each(function(index,group){
				group = $(group);
				group.data("index",index);
				//TODO需要重构Group对象
				var g2 = HteOS.app.AppManager.getGroup(group.data("group"));
				if(g2){
					if(g2.index != index){
						fireEvent = true;
					}
					g2.index = index;
				}
			});
			if(fireEvent === true){
				HteOS.EventManager.trigger("hte.group.sort",me.groups);
			}
		}
	}
}));